package com.sunday.common.base;


import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.View;

import com.sunday.common.event.EventBus;
import com.sunday.common.volley.AuthFailureError;
import com.sunday.common.volley.Request;
import com.sunday.common.volley.RequestQueue;
import com.sunday.common.volley.Response;
import com.sunday.common.volley.toolbox.StringRequest;

import com.sunday.common.widgets.loadingdialog.LoadingDialog;

import java.util.HashMap;
import java.util.Map;

import butterknife.ButterKnife;

/**
 * Created by Administrator on 2015/6/9.
 */
public class BaseFragment  extends Fragment {
    protected Context mContext;
    protected Intent intent;
    protected RequestQueue mRequestQueue;

    protected LoadingDialog progressDialog;
    //标识是否显示对话框
    protected boolean progressShow;
    protected boolean isFinish = false;

    protected View rootView;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
        EventBus.getDefault().unregister(this);
        isFinish = true;
    }

    protected StringRequest post(String url ,final HashMap<String,String> maps,
                        Response.ErrorListener errorListener,
                        Response.Listener response){
        StringRequest request = new StringRequest(Request.Method.POST, url,response,errorListener){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return maps;
            }
        };
        return request;
    }


    protected  void showLoadingDialog(){
        progressShow = true;
        if(progressDialog==null){
            progressDialog = new LoadingDialog(mContext);
            progressDialog.setCanceledOnTouchOutside(false);
            progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {

                @Override
                public void onCancel(DialogInterface dialog) {
                    progressShow = false;
                }
            });
        }
        if(progressDialog!=null&&!progressDialog.isShowing()){
            progressDialog.show();
        }
    }
    protected void dissMissDialog(){
        if(progressDialog!=null){
            progressDialog.dismiss();
        }
    }


}
